﻿using System;
using System.Collections.Generic;
using ProductType = BotonJXC.Entity.ProductType;
using ProductTypeDAO = BotonJXC.DAO.ProductTypeDAO;
using BotonJXC.Util;

namespace BotonJXC.BLL
{
    public class ProductTypeBLL : BaseBLL<ProductTypeDAO, ProductType, int>
    {
        public ProductTypeBLL()
        {

        }
        public ProductType GetByCode(string code)
        {
            return Dao.FindUniqueBy("TypeCode", code);
        }
        public TreeDataNode<ProductType> FindAllToTree()
        {
            IDictionary<string, TreeDataNode<ProductType>> dict = new Dictionary<string, TreeDataNode<ProductType>>();
            TreeDataNode<ProductType> tree = new TreeDataNode<ProductType>();
            IList<ProductType> list = FindAll();
            foreach (ProductType ar in list)
            {
                TreeDataNode<ProductType> arNode = getParentNode(dict, ar.TypePath);
                TreeDataNode<ProductType> arNode2 = new TreeDataNode<ProductType>(ar);
                dict.Add(ar.TypeId.ToString(), arNode2);
                if (arNode == null)
                    tree.Add(arNode2);
                else
                    arNode.Add(arNode2);
            }
            return tree;
        }
        private TreeDataNode<ProductType> getParentNode(IDictionary<string, TreeDataNode<ProductType>> nodes, string path)
        {
            string[] paths = path.Split('-');
            if (paths.Length > 2)
            {
                TreeDataNode<ProductType> ar = null;
                nodes.TryGetValue(paths[paths.Length - 3], out ar);
                return ar;
            }
            return null;
        }
    }
}
